<template>
  <div id="commentExaminedList">
    <h1 class="con-right-title">
      活动帖子评论审核
    </h1>
    <!-- 上部搜索条件 -->
    <top-search :form-inline="formInline" @toSearch="toSearch"></top-search>
    <!-- 表格按钮 -->
    <div class="table-conBtn">
      <el-button type="warning" icon="el-icon-check" @click="pass">
        通过
      </el-button>
      <el-button type="warning" icon="el-icon-close" plain @click="noPass">
        不通过
      </el-button>
      <freshCom @searchList="searchList"></freshCom>
      <selectColumn
        ref="selectCum"
        @setColumn="setColumns"
        :nameList="columnList"
      ></selectColumn>
    </div>
    <!-- 表格部分 -->
    <el-table
      v-loading="loading"
      :data="tableData"
      stripe
      style="width: 100%"
      @selection-change="handleSelectionChange"
      max-height="490"
      ref="table"
    >
      <el-table-column
        type="selection"
        width="50"
        align="center"
        :selectable="selectAble"
      ></el-table-column>
      <el-table-column type="index" width="50" align="center" label="序号">
        <template slot-scope="scope">
          {{ scope.$index | filterPage(searchParams.page, searchParams.size) }}
        </template>
      </el-table-column>
      <el-table-column
        v-if="columnSHow.avatar"
        label="头像"
        min-width="100"
        align="center"
      >
        <template slot-scope="scope">
          <listImgString :imgInfo="scope.row.user.avatar"></listImgString>
        </template>
      </el-table-column>
      <el-table-column
        label="昵称"
        v-if="columnSHow.name"
        prop="user.name"
        min-width="80"
      ></el-table-column>
      <el-table-column
        label="大鹏号"
        v-if="columnSHow.dpAccount"
        prop="user.dpAccount"
        min-width="80"
      ></el-table-column>
      <el-table-column
        label="类型"
        v-if="columnSHow.contentType"
        min-width="80"
      >
        <template slot-scope="scope">
          <span
            v-if="
              scope.row.ext &&
                scope.row.ext.label &&
                scope.row.ext.label.contentType === 'TEXT'
            "
            >图文</span
          >
          <span
            v-if="
              scope.row.ext &&
                scope.row.ext.label &&
                scope.row.ext.label.contentType === 'VIDEO'
            "
            >短视频</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="评论内容"
        v-if="columnSHow.content"
        width="260"
        class-name="text-overflow"
      >
        <template slot-scope="scope">
          <span
            @click="showDetail(scope.row)"
            class="textContent"
            v-html="filterContent(scope.row)"
          ></span>
        </template>
      </el-table-column>
      <el-table-column
        label="视图预览"
        v-if="columnSHow.images"
        min-width="110"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.images && scope.row.images.length > 0">
            <listImgShow :imgInfo="scope.row.images"></listImgShow>
          </span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        label="点赞数"
        v-if="columnSHow.likeCount"
        prop="commentInfo.likeCount"
        min-width="80"
      >
        <template slot-scope="scope">
          <span v-if="!scope.row.commentInfo">0</span>
          <span v-else>{{ scope.row.commentInfo.likeCount }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="回复数"
        v-if="columnSHow.replyCount"
        prop="commentInfo.replyCount"
        min-width="80"
      >
        <template slot-scope="scope">
          <span v-if="!scope.row.commentInfo">0</span>
          <span v-else>{{ scope.row.commentInfo.replyCount }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="评论时间"
        v-if="columnSHow.createdDate"
        prop="createdDate"
        min-width="100"
      ></el-table-column>
      <el-table-column
        label="用户属性"
        v-if="columnSHow.userProperty"
        prop="userProperty"
        width="80"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.userProperty"
            class="tag-span"
            :class="scope.row.userProperty | filterClass('checkUserAttr')"
            >{{ scope.row.userProperty | filterName('checkUserAttr') }}</span
          >
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        label="机审时间"
        v-if="columnSHow.machineAuditTime"
        prop="machineAuditTime"
        min-width="100"
      ></el-table-column>
      <el-table-column
        label="机审结果"
        v-if="columnSHow.machineAuditStatus"
        min-width="100"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.machineAuditStatus === 1"
            class="tag-span primary"
            >通过</span
          >
          <span
            v-else-if="scope.row.machineAuditStatus === 2"
            class="tag-span warning"
            >未通过</span
          >
          <span
            v-else-if="scope.row.machineAuditStatus === 3"
            class="tag-span warning"
            >未审核</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="人工审核结果"
        v-if="columnSHow.peopleAuditStatus"
        min-width="110"
      >
        <template slot-scope="scope">
          <span
            v-if="scope.row.peopleAuditStatus === 1"
            class="tag-span primary"
            >通过</span
          >
          <span
            v-else-if="scope.row.peopleAuditStatus === 2"
            class="tag-span warning"
            >未通过</span
          >
          <span
            v-else-if="scope.row.peopleAuditStatus === 3"
            class="tag-span default"
            >未审核</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="审核人"
        v-if="columnSHow.auditingUserName"
        min-width="80"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.auditingUserName">{{
            scope.row.auditingUserName
          }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column
        label="人工审核时间"
        v-if="columnSHow.peopleAuditTime"
        width="110"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.peopleAuditTime">{{
            scope.row.peopleAuditTime
          }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click="showDetail(scope.row)">
            查看详情
          </el-button>
          <el-button
            type="text"
            @click="changeView(scope.row.ext.originTopicId)"
          >
            评论列表
          </el-button>
          <el-button
            v-if="scope.row.peopleAuditStatus === 3"
            type="text"
            @click="passComment(scope.row.id)"
          >
            通过
          </el-button>
          <el-button
            v-if="scope.row.peopleAuditStatus === 3"
            type="text"
            @click="showFailDialog(scope.row.id)"
          >
            不通过
          </el-button>
          <template v-if="scope.row.commentInfo">
            <el-button
              v-if="
                !scope.row.commentInfo.firstCommentId &&
                  !scope.row.commentInfo.isRecommend
              "
              type="text"
              @click="toTop(scope.row.commentInfo.id)"
            >
              置顶
            </el-button>
            <el-button
              type="text"
              @click="cancelTop(scope.row.commentInfo.id)"
              v-if="
                !scope.row.commentInfo.firstCommentId &&
                  scope.row.commentInfo.isRecommend
              "
              >取消置顶</el-button
            >
            <el-button type="text" @click="showAddPraise(scope.row)">
              加赞
            </el-button>
          </template>
          <el-button
            v-if="scope.row.peopleAuditStatus === 2"
            type="text"
            @click="showReason(scope.row)"
          >
            未通过理由
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-全局组件-->
    <pageCom
      :searchParams="searchParams"
      :total="total"
      @searchList="searchList"
      :pageName="['page', 'size']"
    ></pageCom>
    <!-- 详情弹窗 -->
    <detail-dialog
      :detail-data="detailData"
      v-if="detailData.isShow"
    ></detail-dialog>
    <!-- 加赞弹窗 -->
    <add-praise-dialog
      v-if="addPraiseData.isShow"
      :add-praise-data="addPraiseData"
      @refresh="refresh"
    ></add-praise-dialog>
    <!-- 不通过弹窗 -->
    <del-dialog
      :del-dialog-data="delDialogData"
      @reasonDel="failRequest"
    ></del-dialog>
  </div>
</template>
<script>
import topSearch from '@component/activeManagement/commentExamined/topSearch' // 上部搜索条件
import detailDialog from '@component/activeManagement/commentExamined/detailDialog' // 详情弹窗
import addPraiseDialog from '@component/common/dialogAddPraise' // 加赞弹窗
import delDialog from '@common/delDialog' // 不通过理由弹窗
import { filterCon, getCreateTime, validateOneMonth } from '@utils/pubFunction'
import { saveScrollMixins } from '@/mixins/saveScroll'
import { columnMixin } from '@/mixins/columnShowHide'
import listImgString from '@common/listImgString' // 视图预览(返回值为字符串结构)-公共组件
import {
  getSearchList,
  passSingleAPI,
  passMultAPI,
  failSingleRequestAPI,
  failMultRequestAPI
} from '@/api/commentCheck/activeComment'
import { recommend, cRecommend } from '@/api/common/commentsAPI'
export default {
  name: 'activeManagementCheck',
  msg: '活动帖子评论审核',
  mixins: [saveScrollMixins, columnMixin],
  components: {
    topSearch,
    detailDialog,
    addPraiseDialog,
    delDialog,
    listImgString
  },
  data() {
    return {
      total: 100,
      loading: true,
      formInline: {
        manual: '',
        machine: '',
        sort: '',
        content: '',
        timeType: '',
        postType: '',
        checkUserId: '',
        dpAccount: '',
        userProperty: '', // 用户属性
        startTime: '',
        endTime: '',
        createTime: []
      },
      searchParams: {
        page: 1, // 当前页数
        size: 10 // 页面显示条数
      },
      tableData: [],
      multipleSelection: [],
      detailData: {
        isShow: false,
        id: ''
      },
      addPraiseData: {
        isShow: false,
        likeCount: ''
      },
      delDialogData: {
        isShow: false,
        params: '',
        type: '不通过'
      },
      powerStr: 'system:postsCommentExamined:list', // 是否查询的标识
      powerAry: [], // 可显示的权限
      canShowPage: false, // 是否显示当前页面
      id: '',
      columnList: [
        { label: '头像', value: 'avatar' },
        { label: '昵称', value: 'name' },
        { label: '大鹏号', value: 'dpAccount' },
        { label: '类型', value: 'contentType' },
        { label: '评论内容', value: 'content' },
        { label: '视图预览', value: 'images' },
        { label: '点赞数', value: 'likeCount' },
        { label: '回复数', value: 'replyCount' },
        { label: '评论时间', value: 'createdDate' },
        { label: '用户属性', value: 'userProperty' },
        { label: '机审时间', value: 'machineAuditTime' },
        { label: '机审结果', value: 'machineAuditStatus' },
        { label: '人工审核结果', value: 'peopleAuditStatus' },
        { label: '审核人', value: 'auditingUserName' },
        { label: '人工审核时间', value: 'peopleAuditTime' }
      ]
    }
  },
  activated() {
    this.toTableScrollHeight()
  },
  mounted() {
    this.getCreateTime()
    // this.searchList()
    // this.getPower()
    this.$nextTick(() => {
      setTimeout(() => {
        this.setTableScrollHeight()
        this.$refs['selectCum'].setChoose(this.columnAry)
      }, 1000)
    })
  },
  methods: {
    /* 获取权限 */
    getPower() {
      let id = this.$route.query.mId
      this.getCurPageMenu(id).then((res) => {
        this.powerAry = res
        if (!res.includes(this.powerStr)) {
          this.alertPower(this.powerStr)
        } else {
          this.canShowPage = true
        }
      })
    },
    searchList() {
      let params = { ...this.formInline, ...this.searchParams }
      delete params.createTime
      if (!params.startTime || !params.endTime) {
        this.$message.warning('时间范围不允许为空')
        return
      }
      if (!validateOneMonth(params.startTime, params.endTime)) {
        this.$message.warning('时间范围须在一个月内')
        return
      }
      this.loading = true
      getSearchList(
        params,
        (response) => {
          this.loading = false
          this.tableData = response.rows
          this.total = response.total
        },
        (response) => {
          this.loading = false
          this.$message({
            message: response.message,
            type: 'warning'
          })
        }
      )
    },
    pass() {
      if (this.multipleSelection.length <= 0) {
        this.$message.warning('请至少选择一条数据！')
        return
      }
      this.$confirm(
        `确认要审核通过选中的${this.multipleSelection.length}条数据吗?`,
        '系统提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          let ids = []
          this.multipleSelection.forEach((item) => {
            ids.push(item.id)
          })
          this.passMultRequest(ids)
        })
        .catch(() => {})
    },
    noPass() {
      if (this.multipleSelection.length <= 0) {
        this.$message.warning('请至少选择一条数据！')
        return
      }
      let ids = []
      this.multipleSelection.forEach((item) => {
        ids.push(item.id)
      })
      this.id = ids
      this.delDialogData.isShow = true
    },
    failRequest(reason) {
      if (this.id instanceof Array) {
        this.failMultRequest(this.id, reason)
      } else {
        this.failSingle(this.id, reason)
      }
      this.delDialogData.isShow = false
    },
    // 单个不通过请求
    failSingle(id, reason) {
      let params = {
        reason
      }
      failSingleRequestAPI(id, params, (response) => {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.searchList()
      })
    },
    // 批量不通过请求
    failMultRequest(ids, reason) {
      let params = { ids, reason }
      failMultRequestAPI(params, (response) => {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.searchList()
      })
    },
    // 选择框选中事件
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 分页改变
    changePage(page) {
      this.searchParams.page = page
      this.searchList()
    },
    // 显示未通过理由
    showReason(row) {
      this.$alert(`${row.reason}`, '未通过理由', {
        showConfirmButton: false
      }).catch(() => {})
    },
    // 点击单个评论下通过
    passComment(id) {
      this.$confirm('确定审核通过该条活动帖子评论审核信息吗？', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          passSingleAPI(id, (response) => {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.searchList()
          })
        })
        .catch(() => {})
    },
    // 批量审核通过请求
    passMultRequest(ids) {
      let params = {
        ids
      }
      passMultAPI(params, (response) => {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.searchList()
      })
    },
    // 置顶
    toTop(commentId) {
      this.$confirm('确认要置顶该评论吗？', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let res = await recommend(commentId)
          if (res) {
            this.searchList()
          }
        })
        .catch(() => {})
    },
    // 取消置顶
    cancelTop(commentId) {
      this.$confirm('确认要取消置顶该评论吗?', '系统提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let res = await cRecommend(commentId)
          if (res) {
            this.searchList()
          }
        })
        .catch(() => {})
    },
    // 查看详情
    showDetail(row) {
      let id = row.ext.originTopicId
      this.detailData.id = id
      this.detailData.isShow = true
    },
    // 加赞弹窗
    showAddPraise(row) {
      let likeCount = row.commentInfo.likeCount
      let commentId = row.commentInfo.id
      this.addPraiseData.likeCount = likeCount
      this.addPraiseData.commentId = commentId
      this.addPraiseData.isShow = true
    },
    // 不通过弹窗
    showFailDialog(id) {
      this.id = id
      this.delDialogData.isShow = true
    },
    // 去评论列表
    changeView(id) {
      this.$router.push({
        name: 'wh:activeManagement:commentExamined:secondCommentList',
        query: { id }
      })
    },
    // 搜索
    toSearch() {
      this.searchParams.page = 1
      this.searchList()
    },
    // 过滤评论内容
    filterContent(item) {
      return filterCon(item)
    },
    getCreateTime() {
      const createTime = getCreateTime()
      this.formInline.startTime = createTime[0]
      this.formInline.endTime = createTime[1]
      this.formInline.createTime = createTime
    },
    refresh() {
      this.searchList()
    },
    // 是否可被选择
    selectAble(row, index) {
      if (row.peopleAuditStatus === 3) {
        return true
      } else {
        return false
      }
    }
  }
}
</script>
<style lang="less">
#commentExaminedList {
  @import url('~@common/style/imgBox.less');
  @import url('~@common/style/clamp.less');
  .textContent {
    cursor: pointer;
  }
}
</style>
